SpringBoot+MyBatis+thymeleaf 增删改查实例(超详细) | 您所在的位置:网站首页 › springboot 报表引擎 › SpringBoot+MyBatis+thymeleaf 增删改查实例(超详细) |
导语:
刚学springboot几天,做了个小例子,感觉springboot很好用。相比ssm,springboot除去了很多的繁杂的配置,使用起来非常方便。而且springboot内置了tomcat、jetty这两个web服务器,也不需要你再去配置web服务器,方便刚学springboot或者想学springboot的朋友,下面就带你一步一步做成一个小例子 项目开发环境开发工具:IDEA 2019.2 框架:springboot、mybatis、bootstrap 模板引擎:thymeleaf 数据库:MySQL 构建工具:Maven 可视化工具:Navicat for MySQL 项目搭建项目的创建这里就不详细写了,不会的朋友可以看这里 https://blog.csdn.net/Woo_home/article/details/98860135 一、数据库设计 CREATE TABLE `stus` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(50) DEFAULT NULL, `password` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;创建好数据库之后随便插入几条数据就可以了 在resource中创建application.yml配置文件,有两种文件,一种是properties的一种是yml的,这里使用的是yml的,yml的编写格式很重要,就是冒号后面一定要给一个空格,而且不能按tab键缩进,必须手动缩进,下面是对数据库连接的配置 spring: #数据库配置 datasource: url: jdbc:mysql://localhost:3306/数据库名?useSSL=false driver-class-name: com.mysql.jdbc.Driver username: 用户名 password: 密码 initial-size: 10 max-active: 20 max-idle: 8 min-idle: 8 二、导入项目所需的依赖pom.xml依赖 注意 在pom.xml文件这里有几点需要注意的,就是导入springboot和mybatis的整合包的时候不需要再导入mybatis包,会冲突添加junit测试包的时候不用添加scope,不然无法在controller中使用测试 4.0.0 com.java.springboot springboot-demo 1.0-SNAPSHOT war org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.0 junit junit 三、项目目录结构 com.java.springboot controller(包) UserController.java bean(包) User.java mapper(包) UserMapper.java service(包) UserService.java App.java个人不是很会前端哈,如果写的丑希望大佬们不轻点喷,多给建议 在resource目录下新建两=个文件夹,分别是templates、static templates用于存放html页面 static用于存放静态资源文件,如(js、img、css) 在application.yml配置对应的信息,接着数据库后面添加就行 #静态资源配置 resources: static-locations: classpath:static/ #thymeleaf模板引擎配置 thymeleaf: prefix: classpath:/templates/ suffix: .html mode: HTML5 encoding: UTF-8 content-type: text/html cache: false在templates文件夹中新建一个html页面,命名为userList.html,如果需要使用样式框架的话加在static里面的文件夹就可以了 注意 在html中导入bootstrap样式的时候路径要注意,就是如果你在application.yml配置了访问的是static目录下的资源的时候,在html导入的静态资源是不需要在前面加static了比如这里的bootstrap.css样式是放在static/css目录下的,在html中就直接写css/bootstrap.css就可以了,如果在前面加了static就无法访问样式了,这里踩了很多坑,要注意使用thymeleaf模板引擎的时候一定添加 xmlns:th="http://www.thymeleaf.org"在html标签中 用户列表 a{ color:#fff; } |
CopyRight 2018-2019 实验室设备网 版权所有 |